﻿

/*
* Project: Sherp
* Copyright(c)
* Version: 1.0.0 
* Author: Caijianfeng <sharewind@gmail.com> 
* DateTime: 2007年6月2日 星期六 下午 15:17:44
*/



package com.cse.sherp.action.xitong;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.extremecomponents.table.context.Context;
import org.extremecomponents.table.context.HttpServletRequestContext;
import org.extremecomponents.table.limit.Limit;
import org.extremecomponents.table.limit.LimitFactory;
import org.extremecomponents.table.limit.Sort;
import org.extremecomponents.table.limit.TableLimit;
import org.extremecomponents.table.limit.TableLimitFactory;
import org.tulip.DaoException;

import com.cse.sherp.Constants;
import com.cse.sherp.action.BaseAct;
import com.cse.sherp.business.xitong.GongYi;
import com.cse.sherp.business.xitong.GongYiDao;
import com.cse.sherp.form.xitong.GongYiForm;

import com.cse.sherp.web.ec.ECConstants;

/**
 * 工艺Action
 */
public class GongYiAct extends BaseAct {
	
	@Override
	public ActionForward doAction(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		logger.debug("execute(ActionMapping, ActionForm, HttpServletRequest, HttpServletResponse) - start"); //$NON-NLS-1$
		GongYiForm gongYiForm = (GongYiForm) form;
		if (null != gongYiForm) {
		 				
			// 新增
			if (Constants.ACT_ADD.equals(gongYiForm.getAct())) {
				if (isTokenValid(request)) {
					addGongYi(gongYiForm);
					setFormNull(gongYiForm);
				} else {
					setFormNull(gongYiForm);
					gongYiForm.setMessages("请不要重复提交!");
				}
			}

			// 删除
			else if (Constants.ACT_DEL.equals(gongYiForm
					.getAct())) {
				delGongYi(gongYiForm);
				setFormNull(gongYiForm);
			}
			// 编辑
			else if (Constants.ACT_EDIT.equals(gongYiForm
					.getAct())) {
				editGongYi(gongYiForm);
				gongYiForm.setAct(Constants.ACT_UPDATE);
			}
			// 编辑后保存--更新
			else if (Constants.ACT_UPDATE.equals(gongYiForm
					.getAct())) {
				if (isTokenValid(request)) {
					updateGongYi(gongYiForm);
					//setFormNull(gongYiForm);
				} else {
					setFormNull(gongYiForm);
					gongYiForm.setMessages("请不要重复提交!");
				}

			}
			//默认显示
			else {
				setFormNull(gongYiForm);
			}

		}

		// 显示列表
		displayTable(request);
		
		ActionForward returnActionForward = mapping.findForward("success");

		logger.debug("execute(ActionMapping, ActionForm, HttpServletRequest, HttpServletResponse) - end"); 
		return returnActionForward;
	}
	


	/**
	 * 显示工艺列表
	 */
	private void displayTable(HttpServletRequest request) throws DaoException {
		GongYiDao dao = new GongYiDao();
		Context context = new HttpServletRequestContext(request);
		LimitFactory limitFactory = new TableLimitFactory(context);
		Limit limit = new TableLimit(limitFactory);
		int totalRows = dao.getTotalGongYis();
		limit.setRowAttributes(totalRows,
				ECConstants.PAGE_DEFAULT_ROWS_DISPALAY);

		Sort sort = limit.getSort();
		String prop = sort.getAlias();
		String sortOrder = sort.getSortOrder();
		List gongYis = dao.getGongYis(prop, sortOrder, limit.getCurrentRowsDisplayed(), limit.getRowStart());
		request.setAttribute("totalRows", new Integer(totalRows));
		request.setAttribute("GongYis", gongYis);
	}

    /**
	 * 更新工艺
	 */
	private void updateGongYi(GongYiForm gongYiForm) {
		GongYi gongYi = new GongYi();
		try {
			BeanUtils.copyProperties(gongYi, gongYiForm);
			new GongYiDao().updateGongYi(gongYi);
		} catch (Exception e) {
			gongYiForm.setMessages("更新工艺失败!" + e.getMessage());
			e.printStackTrace();
		}
	}

	/**
	 * 编辑工艺
	 */
	private void editGongYi(GongYiForm gongYiForm) {
		try {
			GongYi obj = new GongYiDao().getGongYiByID(gongYiForm.getGyId());
			
	        gongYiForm.setGyId(obj.getGyId());
	        gongYiForm.setName(obj.getName());
	        gongYiForm.setGylbId(obj.getGylbId());
	        gongYiForm.setDwId(obj.getDwId());
	        gongYiForm.setPrice(obj.getPrice());
	        gongYiForm.setQidianNum(obj.getQidianNum());
	        gongYiForm.setQidianPrice(obj.getQidianPrice());
	        gongYiForm.setMaxKd(obj.getMaxKd());
	        gongYiForm.setMaxSizeLength(obj.getMaxSizeLength());
	        gongYiForm.setMaxSizeWidth(obj.getMaxSizeWidth());
	        gongYiForm.setMinSizeLength(obj.getMinSizeLength());
	        gongYiForm.setMinSizeWidth(obj.getMinSizeWidth());
	        gongYiForm.setGxId(obj.getGxId());
	        gongYiForm.setExpress(obj.getExpress());
	        gongYiForm.setJxId(obj.getJxId());
	        gongYiForm.setJtId(obj.getJtId());
	        gongYiForm.setSfWx(obj.getSfWx());
	        gongYiForm.setBmId(obj.getBmId());
	        gongYiForm.setWxId(obj.getWxId());
	        gongYiForm.setBeiZhu(obj.getBeiZhu());
						
		} catch (DaoException e) {
			gongYiForm.setMessages("查询工艺失败!" + e.getMessage());
			e.printStackTrace();
		}
	}

	/**
	 * 初始化（置空）工艺
	 */
	private void setFormNull(GongYiForm gongYiForm) {
		//gongYiForm.setAct(Constants.ACT_ADD);
		
	    gongYiForm.setGyId(0);
	    gongYiForm.setName(null);
	    gongYiForm.setGylbId(0);
	    gongYiForm.setDwId(0);
	    gongYiForm.setPrice(0.0);
	    gongYiForm.setQidianNum(0.0);
	    gongYiForm.setQidianPrice(0.0);
	    gongYiForm.setMaxKd(0);
	    gongYiForm.setMaxSizeLength(0.0);
	    gongYiForm.setMaxSizeWidth(0.0);
	    gongYiForm.setMinSizeLength(0.0);
	    gongYiForm.setMinSizeWidth(0.0);
	    gongYiForm.setGxId(0);
	    gongYiForm.setExpress(null);
	    gongYiForm.setJxId(0);
	    gongYiForm.setJtId(0);
	    gongYiForm.setSfWx(0);
	    gongYiForm.setBmId(0);
	    gongYiForm.setWxId(0);
	    gongYiForm.setBeiZhu(null);
		
		gongYiForm.setMessages(null);
	}

	/**
	 * 删除工艺
	 */
	private void delGongYi(GongYiForm gongYiForm) {
		GongYi gongYi = new GongYi();
		try {
			BeanUtils.copyProperties(gongYi, gongYiForm);
			new GongYiDao().delGongYi(gongYi);
		} catch (Exception e) {
			gongYiForm.setMessages("删除工艺失败!" + e.getMessage());
			e.printStackTrace();
		}
	}

	/**
	 * 添加工艺
	 */
	private void addGongYi(GongYiForm gongYiForm){
		GongYi gongYi = new GongYi();
		try {
			BeanUtils.copyProperties(gongYi, gongYiForm);
			new GongYiDao().addGongYi(gongYi);
		} catch (Exception e) {
			gongYiForm.setMessages("添加工艺失败!" + e.getMessage());
			e.printStackTrace();
		}
	}


}